//
//  UIFacebookInvitation.m
//  KurtulusLaw
//
//  Created by Mahmood1 on 12/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "UIFacebookInvitation.h"
#import <FacebookSDK/FacebookSDK.h>
#import "Constant.h"

@implementation UIFacebookInvitation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[FBSession activeSession] close];
    
    [FBSession openActiveSessionWithPermissions:[[NSMutableArray alloc] initWithObjects:@"user_photos",@"user_checkins",@"friends_checkins",@"email",@"user_location",@"publish_stream", nil]  allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
        switch (status) {
            case FBSessionStateOpen: {
           
                [self loginedFB];
            }
                break;
            case FBSessionStateClosed:
            case FBSessionStateClosedLoginFailed:{
                // Once the user has logged in, we want them to 
                // be looking at the root view.
                [self loginedFBFail];
            }
                break;
            default:
                break;
            
        }
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc]
                                      initWithTitle:@"Error"
                                      message:error.localizedDescription
                                      delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
            [alertView show];
        }  
    }];
}
 
- (void)publishStory
{
        
    NSMutableDictionary *postParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
     kURL_BASE, @"link",
     @"Facebook SDK for iOS", @"name",
     @"Kurtulus&Partners' Services.", @"caption",
    @"www.kurtuluspartners.com\nModern Turkish Law Firm, I can follow my cases even from their mobile application. \nDefinitely Recommended!!!", @"description",
     nil];
    
    [FBRequestConnection
     startWithGraphPath:@"me/feed"
     parameters:postParams
     HTTPMethod:@"POST"
     completionHandler:^(FBRequestConnection *connection,
                         id result,
                         NSError *error) {
         NSString *alertText;
         if (error) {
             alertText = [NSString stringWithFormat:
                          @"error: domain = %@, code = %d",
                          error.domain, error.code];
         } else {
             alertText = [NSString stringWithFormat:
                          @"Posted action, id: %@",
                          [result objectForKey:@"id"]];
         }
         // Show the result in an alert
         [[[UIAlertView alloc] initWithTitle:@"Result"
                                     message:alertText
                                    delegate:self
                           cancelButtonTitle:@"OK!"
                           otherButtonTitles:nil]
          show];
     }];
//    [FBRequestConnection startForPostStatusUpdate:@"kien test" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
//        
//    } ];
}

- (void) loginedFB {
    // Ask for publish_actions permissions in context
                    // If permissions granted, publish the story
                [self publishStory];

}

- (void)loginedFBFail {
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Alert"
                              message:@"Login Facebook failed"
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
    [alertView show];

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
